﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Geomatics;
using System.IO;
using System.Xml;

namespace PlanningGIS.CoordCovert.model
{
    public enum Ellipsoids
    {
        Beijing54 = Geomatics.Ellipsoids.Krassovsky,
        Xian80  = Geomatics.Ellipsoids.IAG75,
        WGS84 = Geomatics.Ellipsoids.WGS84,
    }

    public class ProGeoConvert
    {
        ProjectionParas projectionPara;
        Ellipsoids ellipsoids = Ellipsoids.WGS84;

        public ProGeoConvert()
        {
            projectionPara = new ProjectionParas();

            projectionPara.projectionMethod = ProjectionMethods.Gauss;//这里可能是Pan's Geomatics软件的一个bug，用Gauss_Kruger反而不对
            projectionPara.K = 1;
            projectionPara.Yconstant = 500000.0;

            try
            {
                string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config.xml";

                if (File.Exists(configPath))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(configPath);

                    XmlNode xmlNode = xmlDoc.SelectSingleNode("//Config//CoordCovert//高斯经纬正反算");

                    string tqt = xmlNode.Attributes["椭球体"].Value;
                    ellipsoids = (Ellipsoids) Enum.Parse(typeof(Ellipsoids), tqt);
                    projectionPara.Yconstant = Double.Parse(xmlNode.Attributes["东偏"].Value);
                }
            }
            catch { }
        }
        /// <summary>
        /// 经纬度转高斯坐标
        /// </summary>
        /// <param name="ellipsoids"></param>
        /// <param name="B"></param>
        /// <param name="L"></param>
        /// <param name="L0"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        public void BL2XY( Ellipsoids ellipsoids, double B, double L, double L0, ref double X, ref double Y)
        {
            Geomatics.Earth earth = new Geomatics.Earth((Geomatics.Ellipsoids)ellipsoids);
            projectionPara.L0 = L0 * Math.PI / 180;

            double H2=0;
            X = 0;
            Y = 0;
            earth.BL2XY(projectionPara, B * Math.PI / 180, L * Math.PI / 180, 0, ref X, ref Y, ref H2);
        }

        /// <summary>
        /// 高斯转经纬度
        /// </summary>
        /// <param name="ellipsoids"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="L0"></param>
        /// <param name="B"></param>
        /// <param name="L"></param>
        public void XY2BL(Ellipsoids ellipsoids, double X, double Y, double L0, ref double B, ref double L)
        {
            Geomatics.Earth earth = new Geomatics.Earth((Geomatics.Ellipsoids)ellipsoids);
            projectionPara.L0 = L0 * Math.PI / 180;
            double H2=0;
            earth.XY2BL(projectionPara, X, Y, 0, ref B, ref L, ref H2);
            B = B * 180 / Math.PI;
            L = L * 180 / Math.PI;
        }

        /// <summary>
        /// 经纬度转高斯坐标
        /// </summary>
        /// <param name="B"></param>
        /// <param name="L"></param>
        /// <param name="L0"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        public void BL2XY(double B, double L, double L0, ref double X, ref double Y)
        {
            Geomatics.Earth earth = new Geomatics.Earth((Geomatics.Ellipsoids)ellipsoids);
            projectionPara.L0 = L0 * Math.PI / 180;

            double H2 = 0;
            X = 0;
            Y = 0;
            earth.BL2XY(projectionPara, B * Math.PI / 180, L * Math.PI / 180, 0, ref X, ref Y, ref H2);
        }

        /// <summary>
        /// 高斯转经纬度
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="L0"></param>
        /// <param name="B"></param>
        /// <param name="L"></param>
        public void XY2BL( double X, double Y, double L0, ref double B, ref double L)
        {
            Geomatics.Earth earth = new Geomatics.Earth((Geomatics.Ellipsoids)ellipsoids);
            projectionPara.L0 = L0 * Math.PI / 180;
            double H2 = 0;
            earth.XY2BL(projectionPara, X, Y, 0, ref B, ref L, ref H2);
            B = B * 180 / Math.PI;
            L = L * 180 / Math.PI;
        }
    }
}
